<%-- 
    Document   : Service
    Created on : Apr 24, 2010, 11:44:49 PM
    Author     : Gavin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.LinkedList" %>

<jsp:useBean id="myAccounts" scope="session" class="accounts.AccountCollectionBean" />

<%
    LinkedList<accounts.BaseAccount> accts = myAccounts.getAccounts();
    accounts.StandardAccount selectedAccount = new accounts.StandardAccount();
    String action = request.getParameter("action") != null ? request.getParameter("action") : "" ;

    if (request.getParameter("selectedIndex") != null) {
        myAccounts.setSelectedIndex(Integer.parseInt(request.getParameter("selectedIndex")));
    }

    if (myAccounts.getSelectedIndex() != -1) {
        selectedAccount = (accounts.StandardAccount)accts.get(myAccounts.getSelectedIndex());

        if (action.equals("editAccount")) {
            selectedAccount.setAccountName(request.getParameter("accountName"));
            selectedAccount.setUsername(request.getParameter("username"));
            selectedAccount.setPassword(request.getParameter("password"));
            selectedAccount.setAccountDescription(request.getParameter("description"));
            selectedAccount.Save();

            response.sendRedirect("dashboard.jsp");
        } else {
%>
<div>
    <form action="Service.jsp" method="POST" enctype="application/x-www-form-urlencoded" onsubmit="return validatePassword();" >
       <table width="100%">
           <tr>
               <td style="width: 33%;">Account Name</td>
               <td style="width: 33%;">
                   <input name="accountName" id="accountName" type="text" value="<%= selectedAccount.getAccountName()%>" />
                   <input type="hidden" name="action" value="editAccount" />
               </td>
               <td rowspan="6" style="width: 33%">
                   <a style="color: white;" onclick="$('#passwordHistoryContainer').toggle('fold');" href="#">Password History</a>
                   <div id="passwordHistoryContainer" style="color: white; height: 100%; width: 100% ;display: none;">
                       <%
                        java.util.ArrayList<String> passwordList = selectedAccount.getPasswordHistory();
                        for(String password : passwordList)
                            {
                                out.println(password + "<br/>");
                            }
                       %>
                   </div>
               </td>
           </tr>
           <tr>
               <td>Username</td>
               <td><input name="username" id="username" type="text" value="<%= selectedAccount.getUsername()%>" /></td>
               
           </tr>
           <tr>
               <td>Password</td>
               <td><input name="password" id="password" type="text" value="<%= selectedAccount.getPassword()%>" /></td>
           </tr>
           <tr>
               <td>Confirm Password</td>
               <td><input name="confirmPassword" id="confirmPassword" type="text" value="<%= selectedAccount.getPassword()%>" /></td>
           </tr>
           <tr>
               <td>Description</td>
               <td><textarea name="description" id="description" rows="2" cols="20"><%= selectedAccount.getAccountDescription()%></textarea></td>
           </tr>
           <tr>
               <td colspan="2"><input type="submit" value="submit" /></td>
           </tr>
       </table>
   </form
</div>
<%
        }
    }
%>